# encoding=utf-8

"""IWebSocket.py
    此处定义常规的python的websocket服务器几个关键动作
"""
import traceback

from seg_common.Proxy import IProxy
from seg_common.LoggingService import ConsoleService


class IWebSocketServer:
    def __init__(self, proxy: IProxy, **kwargs):
        # 网络链接代理类
        self.web_proxy = proxy

        # websocket框架类
        self.io_object = kwargs.get('register', None)

        # log
        # self.logger = ConsoleService.get_logger()

    def register(self, **kwargs):
        """
            此函数用于注册本对象的函数到框架
        :param kwargs: 一些行为调整信息
        :return:
        """
        raise NotImplementedError

    def connect(self):
        """
            websocket的连接事件
        :return:
        """
        raise NotImplementedError

    def disconnect(self):
        """
            websocket的连接断开事件
        :return:
        """
        raise NotImplementedError

    def push(self, topic: str, msg: dict, **kwargs):
        """
            此接口不加入websocket的连接事件
            通常用于向某个主题推送信息
        :param topic: 推送主题
        :param msg: 推送信息
        :param kwargs: 行为控制
        :return:
        """
        raise NotImplementedError

    def msg_arrive_default(self, msg):
        """
            默认的信息到达处理方案
        :param msg: socketio推送的信息
        :return:
        """
        c_m = '[default websocket function]'
        try:
            m = c_m + ", msg is: {}".format(str(msg))
            ConsoleService.console_log(m, ConsoleService.INFO)
        except Exception as e:
            print(traceback.format_exc())
            ConsoleService.console_logger(c_m + ", a msg cannot be serialize, try print")
            print(msg)


